import 'package:basic_common/common_config.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/local_storage.dart';
import 'package:basic_modle/regist_data.dart';
import 'package:basic_modle/regist_data_err.dart';

class SmsCodeController extends GetxController {
  //验证码
  final phoneNumber = ''.obs;

  // 请求类型 1 登录 2 注册
  final type = 0.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    phoneNumber.value = Get.arguments!["number"];
    super.onInit();
    type.value = Get.arguments!["type"];
  }

// 获取验证码
  getSmsCode() async {
    String phone = phoneNumber.value;
    if (phone.isEmpty) {
      return ToastUtils.toast("手机号不能为空");
    }
    HttpData? res = await ApiService().smsReq(data: {"phone": phoneNumber});
    if (res.msg == CommonConfig.successStatus) {
      printInfo(info: "获取成功");
    }
  }

  /// 验证码登录
  void loginBySmsCode(String code) async {
    String phone = phoneNumber.value;
    if (phone.isEmpty) {
      ToastUtils.toast("手机号不能为空");
      return;
    }
    HttpData? httpData = await ApiService().loginReq(data: {
      "scene": "fwapp-dr",
      "scene_desc": "医生端app登录,短信验证码",
      "loginMode": "smcode",
      "phone": phone,
      "smcode": code
    });
    if (httpData.msg == CommonConfig.successStatus) {
      LoginData loginData = LoginData.fromJson(httpData.data);
      if (loginData.accessToken!.isNotEmpty) {
        bool isHasToken = await LocalStorage.remove("accessToken");
        bool isHasLogin = await LocalStorage.remove("isLogin");
        if (isHasToken && isHasLogin) {
          bool isSaveToken =
              await LocalStorage.save("accessToken", loginData.accessToken!);
          bool isLogin = await LocalStorage.save("isLogin", true);
          if (isSaveToken && isLogin) {
            RouterManager().navToMainTab();
          } else {
            ToastUtils.toast("登录失败");
          }
        }
      } else {
        ToastUtils.toast("登录失败");
      }
    }
  }

  /// 注册 校验手机号和验证码
  void validSmsCode(String code) async {
    String phone = phoneNumber.value;
    if (phone.isEmpty) {
     ToastUtils.toast("手机号不能为空");
      return ;
    }
    HttpData res =
        await ApiService().validSmsCode(data: {"phone": phone, "smcode": code});
    if (res.code == 1) {
      register(phone);
    } else {
      ToastUtils.toast(res.msg);
      // Get.offAllNamed(AppRoutes.regist);
      // GetManager().offAllNamed(AppRoutes.regist);
      RouterManager().navToRegister();
    }
  }

  /// 注册
  void register(String phone) async {
    HttpData res = await ApiService().register(data: {"phone": phone});
    if (res.code == 1) {
      if (res.data['resultCode'] != null && res.data['resultCode'] == -1) {
        RegistDataErr dataErr = RegistDataErr.fromJson(res.data);
        ToastUtils.toast(dataErr.resultMsg!);
        Get.offAllNamed(AppRoutes.login);
        // Get.toNamed(AppRoutes.realname,
        //   arguments: {"phone": '15000000005', "id": '100246'});
      } else {
        RegistData regData = RegistData.fromJson(res.data);
        LocalStorage.save("accessToken", regData.accessToken!);

        Get.toNamed(AppRoutes.realname,
            arguments: {"phone": phoneNumber.value, "id": regData.userId});
      }
    } else {
      ToastUtils.toast(res.msg);
    }
  }

}
